﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Admin.CYT369.Com.WebBase;

namespace user.admin.cyt369.com.Areas.UserAdmin.Controllers
{
    public partial class TermsController
    {
        #region Terms
        [HttpGet]
        public ActionResult GetTerms()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;

            string name = Request.Query.GetString("Name");

            
            var items = user.admin.cyt369.com.services.Services.TermsService.Instance.GetTerms(page, rows, ref total, name).ToArray();
            foreach (var item in items)
            {
                item.Content = "";
            }

            return DefaultJson(new { total = total, rows = items });
        }

        [HttpGet]
        public IActionResult GetTermsByID(long id)
        {
            var role = user.admin.cyt369.com.services.Services.TermsService.Instance.GetTermsByID(id);

            return DefaultJson(role);
        }

        [HttpDelete]
        public IActionResult DeleteTerms(long id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (user.admin.cyt369.com.services.Services.TermsService.Instance.DeleteTerms(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public IActionResult SaveTerms([FromForm] user.admin.cyt369.com.services.Models.Terms terms)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();

            if (terms != null)
            {
                terms.EditorUserID = this.GetUserId();
                terms.CreatedTime = DateTime.Now;

                if (user.admin.cyt369.com.services.Services.TermsService.Instance.SaveTerms(terms))
                {
                    resp.Msg = terms.Id.ToString();
                    resp.Result = true;
                }
                else
                {
                    resp.Msg = "保存失败";
                }
            }
            else
            {
                resp.Msg = "参数错误";
            }

            return DefaultJson(resp);
        }

        #endregion

        #region TermsType
        [HttpGet]
        public ActionResult GetTermTypes()
        {
            int page = Request.Query.GetInteger("page", 0);
            int rows = Request.Query.GetInteger("rows", 20);
            int total = 0;

            string name = Request.Query.GetString("Name");


            var items = user.admin.cyt369.com.services.Services.TermsService.Instance.GetTermsTypes(page, rows, ref total);

            return DefaultJson(new { total = total, rows = items });
        }

        public ActionResult GetAllTermTypes()
        {
            var items = user.admin.cyt369.com.services.Services.TermsService.Instance.GetAllTermsTypes();

            return DefaultJson(items);
        }

        [HttpGet]
        public IActionResult GetTermsTypeByID(int id)
        {
            var role = user.admin.cyt369.com.services.Services.TermsService.Instance.GetTermsTypeByID(id);

            return DefaultJson(role);
        }

        [HttpDelete]
        public IActionResult DeleteTermsType(int id)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            if (user.admin.cyt369.com.services.Services.TermsService.Instance.DeleteTermsType(id))
            {
                resp.Result = true;
            }
            else
            {
                resp.Msg = "删除失败";
            }

            return DefaultJson(resp);
        }

        [HttpPost]
        public IActionResult SaveTermsType([FromForm] user.admin.cyt369.com.services.Models.TermsType termsType)
        {
            var resp = new Admin.CYT369.Com.WebBase.ExecuteResult();
            termsType.ChangedTime = DateTime.Now;

            if (termsType != null)
            {
                if (user.admin.cyt369.com.services.Services.TermsService.Instance.SaveTermsType(termsType))
                {
                    resp.Msg = termsType.Id.ToString();
                    resp.Result = true;
                }
                else
                {
                    resp.Msg = "保存失败";
                }
            }
            else
            {
                resp.Msg = "参数错误";
            }

            return DefaultJson(resp);
        }

        #endregion
    }
}